package com.xiaoyi.intentsdklibrary.ThreadUtil;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class AdvancedThreadRunnable implements Runnable {
    private static final int MAX_NO_DATA_COUNTER = 6;
    private volatile boolean aliveFlag = false;
    private volatile long delay = 1000;
    private volatile boolean isEnableDelay = false;
    private RunnableJob job = null;
    private Object lockObj = new Object();
    private String name = "";
    private AtomicInteger nodataCounter = new AtomicInteger(0);
    private volatile boolean stopFlag = false;
    private volatile boolean suspendFlag = false;

    public static class DefaultRunnableJob implements RunnableJob {
        @Override // com.xiaoyi.intentsdklibrary.ThreadUtil.AdvancedThreadRunnable.RunnableJob
        public void doDispose() {
        }

        @Override // com.xiaoyi.intentsdklibrary.ThreadUtil.AdvancedThreadRunnable.RunnableJob
        public void doException(Object obj, Throwable th) {
        }

        @Override // com.xiaoyi.intentsdklibrary.ThreadUtil.AdvancedThreadRunnable.RunnableJob
        public void doFinish() {
        }

        @Override // com.xiaoyi.intentsdklibrary.ThreadUtil.AdvancedThreadRunnable.RunnableJob
        public void doPrepare() {
        }

        @Override // com.xiaoyi.intentsdklibrary.ThreadUtil.AdvancedThreadRunnable.RunnableJob
        public List<?> getWorkItems() {
            return null;
        }

        @Override // com.xiaoyi.intentsdklibrary.ThreadUtil.AdvancedThreadRunnable.RunnableJob
        public RunnableJobStatus run(Object obj) throws ExecuteException {
            return null;
        }

        @Override // com.xiaoyi.intentsdklibrary.ThreadUtil.AdvancedThreadRunnable.RunnableJob
        public int runTimes() {
            return 0;
        }
    }

    public interface RunnableJob {
        void doDispose();

        void doException(Object obj, Throwable th);

        void doFinish();

        void doPrepare();

        List<?> getWorkItems();

        RunnableJobStatus run(Object obj) throws ExecuteException;

        int runTimes();
    }

    public enum RunnableJobStatus {
        SUCCESS,
        FAILURE,
        NODATA
    }

    public AdvancedThreadRunnable() {
    }

    public AdvancedThreadRunnable(String str) {
        this.name = str;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String str) {
        this.name = str;
    }

    public RunnableJob getJob() {
        return this.job;
    }

    public void setJob(RunnableJob runnableJob) {
        this.job = runnableJob;
    }

    public void run() {
        if (this.job != null) {
            this.aliveFlag = true;
            this.job.doPrepare();
            try {
                int runTimes = this.job.runTimes();
                if (runTimes == -1) {
                    while (!this.stopFlag) {
                        synchronized (this.lockObj) {
                            if (this.suspendFlag) {
                                try {
                                    this.lockObj.wait();
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                        try {
                            List<?> workItems = this.job.getWorkItems();
                            if (workItems != null) {
                                for (Object obj : workItems) {
                                    if (this.stopFlag) {
                                        break;
                                    }
                                    synchronized (this.lockObj) {
                                        if (this.suspendFlag) {
                                            try {
                                                this.lockObj.wait();
                                            } catch (InterruptedException e2) {
                                                e2.printStackTrace();
                                            }
                                        }
                                    }
                                    try {
                                        this.job.run(obj);
                                    } catch (Exception e3) {
                                        this.job.doException(obj, e3);
                                    }
                                    if (this.isEnableDelay) {
                                        sleep(this.delay);
                                    } else {
                                        sleep(10);
                                    }
                                }
                                if (this.isEnableDelay) {
                                    sleep(this.delay);
                                } else {
                                    sleep(100);
                                }
                            }
                        } catch (Exception e4) {
                            this.job.doException(null, e4);
                        }
                    }
                    this.job.doFinish();
                    this.aliveFlag = false;
                    this.job.doDispose();
                }
                for (int i = 0; i < runTimes && !this.stopFlag; i++) {
                    synchronized (this.lockObj) {
                        if (this.suspendFlag) {
                            try {
                                this.lockObj.wait();
                            } catch (InterruptedException e5) {
                                e5.printStackTrace();
                            }
                        }
                    }
                    List<?> workItems2 = this.job.getWorkItems();
                    if (workItems2 != null) {
                        for (Object obj2 : workItems2) {
                            if (this.stopFlag) {
                                continue;
                            }
                            synchronized (this.lockObj) {
                                if (this.suspendFlag) {
                                    try {
                                        this.lockObj.wait();
                                    } catch (InterruptedException e6) {
                                        e6.printStackTrace();
                                    }
                                }
                            }
                            try {
                                this.job.run(obj2);
                            } catch (Exception e7) {
                                this.job.doException(obj2, e7);
                            }
                            if (this.isEnableDelay) {
                                sleep(this.delay);
                            } else {
                                sleep(10);
                            }
                        }
                    }
                }
                this.job.doFinish();
                this.aliveFlag = false;
                this.job.doDispose();
            } finally {
                this.aliveFlag = false;
                this.job.doDispose();
            }
        }
    }

    public boolean isStop() {
        return this.stopFlag;
    }

    public boolean isSuspend() {
        return this.suspendFlag;
    }

    public boolean isAlive() {
        return this.aliveFlag;
    }

    public void requestStop() {
        this.stopFlag = true;
    }

    public void requestSuspend() {
        this.suspendFlag = true;
    }

    public void requestResume() {
        if (this.suspendFlag) {
            synchronized (this.lockObj) {
                this.lockObj.notifyAll();
            }
            this.suspendFlag = false;
        }
    }

    public long getDelay() {
        return this.delay;
    }

    public void setDelay(long j) {
        this.delay = j;
    }

    public boolean isEnableDelay() {
        return this.isEnableDelay;
    }

    public void setEnableDelay(boolean z) {
        this.isEnableDelay = z;
    }

    public static void sleep(long j) {
        try {
            Thread.sleep(j);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
